package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextField;

import bus.QuizzesBus;
import dao.SubjectDao;
import dto.SubjectDto;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

public class JFrameAdminQuizManagement extends JFrame {

	private JPanel contentPane;
	private JCheckBox ckbIsAvailable;
	private JComboBox ccbSubject;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrameAdminQuizManagement frame = new JFrameAdminQuizManagement();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JFrameAdminQuizManagement() {
		setTitle("Admin - Add quiz to subject");
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				JFrameAdminMainPage jFrameAdminMainPage = new JFrameAdminMainPage();
				jFrameAdminMainPage.setVisible(true);
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
				SubjectDao subjectDao = new SubjectDao();
				ArrayList<SubjectDto> allSubject = subjectDao.getAllSubject();
				for (SubjectDto subjectDto : allSubject) {
					ccbSubject.addItem(subjectDto.getSubjectName());
				}

			}
		});
		setResizable(false);
		setBounds(100, 100, 500, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblQuizManagement = new JLabel("Quiz Management");
		lblQuizManagement.setHorizontalAlignment(SwingConstants.CENTER);
		lblQuizManagement.setFont(new Font("Tahoma", Font.BOLD, 30));
		lblQuizManagement.setBounds(50, 10, 400, 50);
		contentPane.add(lblQuizManagement);

		ckbIsAvailable = new JCheckBox("Quiz is available?");
		ckbIsAvailable.setSelected(true);
		ckbIsAvailable.setFont(new Font("Tahoma", Font.PLAIN, 15));
		ckbIsAvailable.setBounds(129, 163, 200, 23);
		contentPane.add(ckbIsAvailable);

		ccbSubject = new JComboBox();
		ccbSubject.setFont(new Font("Tahoma", Font.PLAIN, 15));
		ccbSubject.setBounds(129, 112, 280, 30);
		contentPane.add(ccbSubject);

		JButton btnAddQuiz = new JButton("Add Quiz To System");
		btnAddQuiz.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (checkDataInput()) {
					showMessage(QuizzesBus.addQuiz(
							(String) ccbSubject.getSelectedItem(),
							ckbIsAvailable.isSelected()));
				}
			}
		});
		btnAddQuiz.setFont(new Font("Tahoma", Font.PLAIN, 18));
		btnAddQuiz.setBounds(129, 266, 280, 60);
		contentPane.add(btnAddQuiz);

		JLabel lblSubject = new JLabel("Subject");
		lblSubject.setHorizontalAlignment(SwingConstants.RIGHT);
		lblSubject.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblSubject.setBounds(19, 112, 100, 30);
		contentPane.add(lblSubject);
	}

	private void showMessage(String mess) {
		JOptionPane.showMessageDialog(this, mess);
	}

	private boolean checkDataInput() {
		return true;
	}

}
